package com.zrf.gulimall.ware.service.impl;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zrf.gulimall.commnon.core.utils.R;
import com.zrf.gulimall.common.database.utils.PageUtils;
import com.zrf.gulimall.common.database.utils.Query;
import com.zrf.gulimall.ware.dao.WareInfoDao;
import com.zrf.gulimall.ware.entity.WareInfoEntity;
import com.zrf.gulimall.ware.feign.MemberFeignService;
import com.zrf.gulimall.ware.service.WareInfoService;
import com.zrf.gulimall.ware.vo.FareVo;
import com.zrf.gulimall.ware.vo.MemberAddressVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Map;


@Service("wareInfoService")
public class WareInfoServiceImpl extends ServiceImpl<WareInfoDao, WareInfoEntity> implements WareInfoService {
	@Autowired
	private MemberFeignService memberFeignService;

	@Override
	public PageUtils queryPage(Map<String, Object> params) {
		String key = (String) params.get("key");
		IPage<WareInfoEntity> page = this.lambdaQuery()
				.and(StrUtil.isNotEmpty(key), wrapper -> wrapper.eq(WareInfoEntity::getId, key)
						.or().like(WareInfoEntity::getName, key)
						.or().like(WareInfoEntity::getAreacode, key))
				.page(new Query<WareInfoEntity>().getPage(params));

		return new PageUtils(page);
	}

	@Override
	public FareVo getFare(Long addrId) {
		FareVo fareVo = new FareVo();

		//收获地址的详细信息
		R addrInfo = memberFeignService.info(addrId);
		MemberAddressVo memberAddressVo = addrInfo.getData("memberReceiveAddress",new TypeReference<MemberAddressVo>() {});
		if (memberAddressVo != null) {
			// 随机给个值给运费
			BigDecimal bigDecimal = RandomUtil.randomBigDecimal(BigDecimal.TEN);
			fareVo.setFare(bigDecimal);
			fareVo.setAddress(memberAddressVo);
			return fareVo;
		}
		return null;
	}

}